/// <remarks>
/// CheckIn Class
/// This form list all files with pending status to be checked In.
/// 
/// Author: Louis-Philippe Lebouthillier, Matricis Informatique Inc.
/// </remarks>

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Common;
using Microsoft.TeamFoundation.VersionControl.Client;
using VcLib;

namespace VcGUI
{
    public partial class CheckInFrm : Form
    {
        private Logger logger = Logger.Instance;
        private VcLib.TFSFacade VcUtil = VcLib.TFSFacade.Instance;
        private PendingChange[] pcs;

        public CheckInFrm(string project, string workspacePath)
        {
            InitializeComponent();
            this.txtProject.Text = project;
            this.txtFolder.Text = workspacePath;
            this.AcceptButton = this.btnOK;
            this.CancelButton = this.btnCancel;
            
            FillFilesList();
            SetListView();
        }

        private void CheckUncheckAll(bool check)
        {
            for (int i = 0; i < this.lstFileCheckIn.Items.Count; i++)
            {
                this.lstFileCheckIn.Items[i].Checked = check;
            } 
        }

        private void btnCheckAll_Click(object sender, EventArgs e)
        {
            CheckUncheckAll(true);
        }

        private void btnUncheckAll_Click(object sender, EventArgs e)
        {
            CheckUncheckAll(false);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SetListView()
        {
            ColumnHeader colHead;

            colHead = new ColumnHeader();
            colHead.Text = "Select";
            colHead.Width = 50;
            this.lstFileCheckIn.Columns.Add(colHead);

            colHead = new ColumnHeader();
            colHead.Text = "State";
            colHead.Width = 80;
            this.lstFileCheckIn.Columns.Add(colHead);

            colHead = new ColumnHeader();
            colHead.Text = "File";
            colHead.Width = 700;
            this.lstFileCheckIn.Columns.Add(colHead);

            colHead = new ColumnHeader();
            colHead.Text = "Date created";
            colHead.Width = 130;
            this.lstFileCheckIn.Columns.Add(colHead);

        }

        private void AddOneFile(PendingChange pc)
        {
            ListViewItem lvi = new ListViewItem();
            ListViewItem.ListViewSubItem lvsi;

            lvi.Checked = true;

            lvsi = new ListViewItem.ListViewSubItem();
            lvsi.Text = pc.ChangeTypeName;
            lvi.SubItems.Add(lvsi);

            lvsi = new ListViewItem.ListViewSubItem();
            lvsi.Text = pc.LocalOrServerFolder + '\\' + pc.FileName;
            lvi.SubItems.Add(lvsi);

            lvsi = new ListViewItem.ListViewSubItem();
            lvsi.Text = pc.CreationDate.ToString();
            lvi.SubItems.Add(lvsi);

            this.lstFileCheckIn.Items.Add(lvi);
        }

        private void FillFilesList()
        {
            try
            {
                this.logger.Info("Start refresh");
                this.lstFileCheckIn.Clear();
                this.lstFileCheckIn.BeginUpdate();
                
                pcs = VcUtil.GetPendingChanges(txtFolder.Text);

                foreach (PendingChange pc in pcs)
                {
                    AddOneFile(pc);
                }

                for (int i = 0; i < this.lstFileCheckIn.Items.Count; i++)
                {
                    CheckUncheckAll(true);
                }

                this.lstFileCheckIn.EndUpdate();
                this.lstFileCheckIn.Refresh();
                this.lstFileCheckIn.View = View.Details;
            }
            catch (Exception ex)
            {
                this.logger.Error(ex.Message);
            }
        }
        
        private void btnOK_Click(object sender, EventArgs e)
        {
            int size = this.lstFileCheckIn.Items.Count;
            int counter = 0;
            PendingChange[] tempPcs = new PendingChange[this.lstFileCheckIn.CheckedItems.Count];
            
            if (tempPcs.Length == 0)
            {
                this.logger.Error("no file to CheckIn");
                return;
            }

            try
            {
                for (int i = 0; i < size; i++)
                {
                    if (this.lstFileCheckIn.Items[i].Checked == true)
                    {
                        tempPcs[counter] = pcs[i];
                        counter++;
                    }
                }
                this.logger.Info("Doing CheckIn");
                this.logger.ServerInfo("Check-In Successfull! ChangeSet: " + VcUtil.DoCheckIn(tempPcs, txtFolder.Text, txtComment.Text).ToString() + " Created");
                this.Close();
            }
            catch (Microsoft.TeamFoundation.VersionControl.Client.CheckinException)
            {
                this.logger.Error("Conflicts detected, do Get Latest first.");
            }
        }
    }
}